package com.koron.system.agent.business.service.impl;

import com.koron.bean.system.agent.po.AgentRoleUserPo;
import com.koron.bean.system.agent.po.AgentUserPo;
import com.koron.bean.system.agent.vo.AgentUserVo;
import com.koron.bean.system.org.OrgVo;
import com.koron.system.agent.bean.convertor.AgentConvertor;
import com.koron.system.agent.bean.query.AgentUserQuery;
import com.koron.system.agent.business.bean.dto.AgentUserDto;
import com.koron.system.agent.business.bean.vo.AgentInfo;
import com.koron.system.agent.business.bean.vo.AgentVo;
import com.koron.system.agent.business.mapper.AgentMapper;
import com.koron.system.agent.business.service.AgentService;
import com.koron.system.agent.service.AgentRoleUserService;
import com.koron.system.agent.service.AgentUserService;
import com.koron.system.staff.service.StaffService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @ClassName AgentServiceImpl
 * @description
 * @Author zhouj
 * @Date 2021/11/23 19:03
 */
@Service
public class AgentServiceImpl implements AgentService {

    @Autowired
    AgentUserService agentUserService;

    @Autowired
    AgentRoleUserService agentRoleUserService;

    @Autowired
    AgentMapper agentMapper;

    @Autowired
    StaffService staffService;

    @Autowired(required=false)
    AgentConvertor agentConvertor;

    @Override
    public int cancel(String leader, String agent) {
        //将代理用户数据删除
        int i = agentUserService.deleteByAgent(leader, agent);

        //删除该代理用户角色数据
        agentRoleUserService.deleteByAgent(leader, agent);

        return i;
    }

    @Override
    public List<AgentVo> queryAgentList(String leader) {
        List<AgentVo> agentVos = agentMapper.queryAgentList(leader);

        if (CollectionUtils.isNotEmpty(agentVos)) {

            agentVos.forEach(p -> {
                String agent = p.getAgent();
                List<OrgVo> orgVos = staffService.queryOrgVoListByAccount(agent);
                p.setOrgVoList(orgVos);
                p.setAgentStatus(0);
                if (p.getAging() != null && p.getAging() == 1) {
                    p.setAgentStatus(1);
                } else {
                    Date expired = p.getExpired();
                    if (!Objects.isNull(expired) && expired.getTime() > System.currentTimeMillis()) {
                        p.setAgentStatus(1);
                    }
                }
            });
        }

        return agentVos;
    }

    @Override
    public AgentInfo queryAgentInfo(String leader) {

        AgentInfo agentInfo = new AgentInfo();

        //上级
        AgentUserQuery agentUserQueryPre = new AgentUserQuery();
        agentUserQueryPre.setAgent(leader);
        agentUserQueryPre.setAgentStatus(1);
        List<AgentUserVo> agentLeaderUserPos = agentUserService.queryVoList(agentUserQueryPre);
        agentInfo.setAgentLeaderPoList(agentLeaderUserPos);

        //下级
        AgentUserQuery agentUserQuerySuf = new AgentUserQuery();
        agentUserQuerySuf.setLeader(leader);
        agentUserQuerySuf.setAgentStatus(1);
        List<AgentUserVo> agentUserPos = agentUserService.queryVoList(agentUserQuerySuf);
        agentInfo.setAgentUserPoList(agentUserPos);

        return agentInfo;
    }

    @Override
    public int authorize(AgentUserDto agentUserDto) {

        AgentUserPo agentUserPo = agentConvertor.agentUserDtoToPo(agentUserDto);

        int i = agentUserService.insertOrUpdate(agentUserPo);

        List<String> roleIds = agentUserDto.getRoleIds();

        if (CollectionUtils.isNotEmpty(roleIds)) {

            List<String> distinct = roleIds.stream().distinct().collect(Collectors.toList());

            List<AgentRoleUserPo> list = new ArrayList<>();
            //入库代理角色
            distinct.forEach(p -> {
                AgentRoleUserPo agentRoleUserPo = new AgentRoleUserPo();
                agentRoleUserPo.setLeader(agentUserDto.getLeader());
                agentRoleUserPo.setAgent(agentUserDto.getAgent());
                agentRoleUserPo.setRoleId(p);
                list.add(agentRoleUserPo);
            });

            agentRoleUserService.deleteByAgent(agentUserDto.getLeader(), agentUserDto.getAgent());
            agentRoleUserService.insertBatch(list);

        }

        return i;
    }

}
